from django.db import models
from django.contrib.auth.models import User

# 定义等级选择，将中文等级映射为数字
LEVEL_CHOICES = (
    (1, '一级'),
    (2, '二级'),
    (3, '三级'),
    (4, '四级'),
    (5, '五级'),
    (6, '六级'),
    (7, '七-九级'),
)

class Word(models.Model):
    word = models.CharField(max_length=100)
    # 将 level 字段改为 IntegerField 类型
    level = models.IntegerField(choices=LEVEL_CHOICES)
    # 添加例句字段
    example_sentence = models.TextField(verbose_name='例句', blank=True, null=True)

    def __str__(self):
        return self.word

class CharacterFrequency(models.Model):
    """
    字频表模型，用于存储字及其对应的字频。
    """
    character = models.CharField(max_length=1, verbose_name='字')
    frequency = models.IntegerField(verbose_name='字频')

    def __str__(self):
        return f"{self.character}: {self.frequency}"

    class Meta:
        verbose_name = '字频表'
        verbose_name_plural = '字频表'

class GeneralFrequency(models.Model):
    """
    通用字频表模型，用于存储通用字及其对应的字频。
    """
    character = models.CharField(max_length=1, verbose_name='字')
    frequency = models.IntegerField(verbose_name='字频')

    def __str__(self):
        return f"{self.character}: {self.frequency}"

    class Meta:
        verbose_name = '通用字频表'
        verbose_name_plural = '通用字频表'

class TermDatabase(models.Model):
    term_name = models.CharField(max_length=200, verbose_name='术语名称')
    term_introduction = models.TextField(verbose_name='术语介绍')
    term_field = models.CharField(max_length=200, verbose_name='术语领域')
    term_code = models.CharField(max_length=50, verbose_name='术语代码')
    is_out_of_scope = models.IntegerField(choices=[(0, '不超纲'), (1, '超纲')], verbose_name='术语是否超纲')
    has_focus = models.BooleanField(verbose_name='义项有无侧重')
    is_truly_out_of_scope = models.IntegerField(choices=[(0, '假性超纲'), (1, '真性超纲')], verbose_name='是否真性超纲')
    semantic_transparency = models.CharField(max_length=50, verbose_name='语义透明度')
    # 添加是否通用字段
    is_general = models.IntegerField(
        choices=[(0, '不通用'), (1, '通用')],
        verbose_name='是否通用',
        default=0  # 设置默认值为 0
    )

    def __str__(self):
        return self.term_name

    class Meta:
        verbose_name = '术语数据库'
        verbose_name_plural = '术语数据库'

class Subject(models.Model):
    code = models.CharField(max_length=20)
    name = models.CharField(max_length=200)

    def __str__(self):
        return f"{self.code} - {self.name}"

class UserCollection(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    term = models.ForeignKey('TermDatabase', on_delete=models.CASCADE, null=True, blank=True)
    word = models.ForeignKey('Word', on_delete=models.CASCADE, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        if self.term:
            return f"{self.user.username} 收藏了术语: {self.term.term_name}"
        elif self.word:
            return f"{self.user.username} 收藏了单词: {self.word.word}"
        return f"{self.user.username} 的收藏记录"

class UserBrowseHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    term = models.ForeignKey(TermDatabase, on_delete=models.CASCADE, null=True, blank=True)
    word = models.ForeignKey(Word, on_delete=models.CASCADE, null=True, blank=True)
    browsed_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        if self.term:
            return f"{self.user.username} 浏览了术语: {self.term.term_name}"
        elif self.word:
            return f"{self.user.username} 浏览了单词: {self.word.word}"
        return f"{self.user.username} 的浏览记录"

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    chinese_level = models.CharField(max_length=10, choices=LEVEL_CHOICES)
    subject = models.ForeignKey(Subject, on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return self.user.username